package com.codemart.lanze.modules.cms.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codemart.lanze.modules.cms.entity.Navigation;
import com.codemart.lanze.modules.cms.mapper.NavigationMapper;
import com.codemart.lanze.modules.cms.service.INavigationService;
import com.codemart.lanze.modules.sys.vo.TreeNode;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @Description: 导航栏
 * @Author: lanze
 * @Date: 2019-09-13
 * @Version: V1.0
 */
@Service
public class NavigationServiceImpl extends ServiceImpl<NavigationMapper, Navigation> implements INavigationService {

	@Autowired
	NavigationMapper navigationMapper;

	@Override
	public Navigation findByName(String name) {
		QueryWrapper<Navigation> ew = new QueryWrapper<>();
		ew.eq("name", name);
		return this.getOne(ew);
	}

	@Transactional(readOnly = true)
	@Override
	public List<TreeNode> findTreeList() {
		return navigationMapper.findTreeList();
	}

	/**
	 * 根据用户ID查询用户菜单列表
	 *
	 * @param UserId
	 * @return
	 */
	@Override
	@Transactional(readOnly = true)
	public IPage<Navigation> findPage(Integer pageNum, Integer pageSize, String name) {
		QueryWrapper<Navigation> queryWrapper = new QueryWrapper<>();

		if (StringUtils.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		IPage<Navigation> iPage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
		return iPage;
	}

	// /**
	// * 根据资源类型查询权限列表
	// *
	// * @param type
	// * @return
	// */
	// @Transactional(readOnly = true)
	// public List<Navigation> findListByType(String type) {
	// QueryWrapper<Navigation> queryWrapper = new QueryWrapper<>();
	//
	// if (StringUtils.isNotEmpty(type)) {
	// queryWrapper.eq("type", type);
	// }
	// return this.list(queryWrapper);
	// }

	/**
	 * 根据资源名称模糊匹配
	 *
	 * @param menuName
	 * @return
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Navigation> findListByName(String searchKey) {
		QueryWrapper<Navigation> queryWrapper = new QueryWrapper<>();

		if (StrUtil.isNotEmpty(searchKey)) {
			queryWrapper.like("name", searchKey);
		}
		return this.list(queryWrapper);
	}

}
